<template>
	<div class="sys-pos-container">
		<el-card shadow="hover">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label-width='70' label="媒体名称">
					<el-select :clearable='true' class='ml10' v-model="state.queryParams.mediaId" placeholder="请选择">
						<el-option v-for='item in state.dimediaList' :key='item.value' :label='item.label'
							:value='item.value' />
					</el-select>
				</el-form-item>
				<!-- <el-form-item :label-width='70' label="媒体名称" prop="pid">
					<el-input placeholder="媒体名称" clearable class='ml10' @keyup.enter="handleQuery"
						v-model="state.queryParams.mediaName" />
				</el-form-item> -->
				<el-form-item :label-width='70' label="Pid" prop="pid">
					<el-input placeholder="Pid" clearable class='ml10' @keyup.enter="handleQuery"
						v-model="state.queryParams.pid" />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'diMediaToucher:query'"> 查询
						</el-button>
						<el-button icon="ele-Refresh" @click="resetQuery" v-auth="'diMediaToucher:query'"> 重置 </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="success" plain icon="ele-Plus" @click="openAdd" v-auth="'diMediaToucher:add'"> 新增
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card shadow="hover" style="margin-top: 8px">
			<el-table :data="state.data" style="width: 100%" v-loading="state.loading" border stripe highlight-current-row>
				<el-table-column header-align='center' type="index" label="序号" align="center" width="55" />
				<el-table-column header-align='center' prop="mediaName"  label="媒体名称" width="150" show-overflow-tooltip />
				<el-table-column header-align='center' prop="mediaPositionName" label="代理资源" width="260"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="pid" label="Pid" width="200" show-overflow-tooltip />
				<el-table-column header-align='center' label="状态" align="center" width="80" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
						<el-tag type="danger" v-else-if="scope.row.status === 2">停用</el-tag>
						<el-tag type="warning" v-else-if="scope.row.status === 3">到量</el-tag>
						<el-tag type="warning" v-else-if="scope.row.status === 4">短期关停</el-tag>
						<el-tag type="info" v-else>未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column header-align='center' align="right" prop="deductionNumerator" label="扣除分子" width="90"
					show-overflow-tooltip />
				<el-table-column header-align='center' align="right" prop="deductionDenominator" label="扣除分母" width="90"
					show-overflow-tooltip />
				<el-table-column header-align='center' label="推广链接" width="340" show-overflow-tooltip>
					<template #default="scope">
						<el-link type="info" class="font12" :href="scope.row.promotionLink" target="_black">
							{{ scope.row.promotionLink }}
						</el-link>
					</template>
				</el-table-column>
				<el-table-column header-align='center' prop="remark" label="备注" min-width="200" show-overflow-tooltip />
				<el-table-column header-align='center' label="操作" width="140" fixed="right" align="center"
					show-overflow-tooltip>
					<template #default="scope">
						<el-button size="small" text type="primary" @click="openEdit(scope.row, 'details')"
							v-auth="'diMediaToucher:update'"> 详情 </el-button>
						<el-button size="small" text type="primary" @click="openEdit(scope.row, 'edit')"
							v-auth="'diMediaToucher:update'"> 编辑 </el-button>
						<el-button size="small" text type="danger" @click="del(scope.row)" v-auth="'diMediaToucher:delete'">
							删除 </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
				v-model:page-size="state.queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.total">
			</el-pagination>
		</el-card>
		<EditDiMediaToucher ref="editDiMediaToucherRef" :title="state.editTitle" />
	</div>
</template>

<script lang="ts" setup name="diMediaToucher">
import { onMounted, onUnmounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import EditDiMediaToucher from '/@/views/delivery/operate/mediaToucher/component/editDiMediaToucher.vue';

import { DiMediaToucher } from '/@/api/models';
import SysDiMediaToucherApi from '/@/api/delivery/mediaToucher';
import SysDiMediaApi from '/@/api/delivery/media';

const editDiMediaToucherRef = ref<InstanceType<typeof EditDiMediaToucher>>();
const state = reactive({
	loading: false,
	data: [] as Array<DiMediaToucher>,
	total: 0 as any,
	queryParams: {
		pid: undefined,
		pageNum: 1,
		pageSize: 20,
		mediaName: undefined
	},
	editTitle: '',
	dimediaList: []
});

onMounted(async () => {
	mittBus.on('submitRefresh', () => {
		handleQuery();
	});
	await resSysDiMediaApi();
});

onUnmounted(() => {
	mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	const res = await SysDiMediaToucherApi().queryRecord(state.queryParams);
	state.data = res.data.result.items ?? [];
	state.total = res.data.result?.total;
	state.loading = false;
};

// 重置操作。。
const resetQuery = () => {
	state.queryParams.pid = undefined;
	handleQuery();
};

// 打开新增页面
const openAdd = () => {
	state.editTitle = '添加';
	editDiMediaToucherRef.value?.openDialog({ status: 1, deductionDenominator: 10, deductionNumerator: 0,blackApp:-1 }, 'add');
};

// 打开编辑页面
const openEdit = (row: DiMediaToucher, ages: string | null) => {
	state.editTitle = '编辑';
	if (ages != null) state.editTitle = '详情';
	editDiMediaToucherRef.value?.openDialog(row, ages);
};

// 删除
const del = (row: any) => {
	ElMessageBox.confirm(`确定删除PID为：【${row.pid}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res = await SysDiMediaToucherApi().deletePost({ id: row.id });
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else
				ElMessage.success("删除成功");
			await handleQuery();
		})
		.catch(() => { });
};

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.queryParams.pageSize = val;
	handleQuery();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.queryParams.pageNum = val;
	handleQuery();
};

const resSysDiMediaApi = async () => {
	await SysDiMediaApi().queryAll().then((res) => {
		if (res.data.resCode === '0000')
			state.dimediaList = res.data.result ?? [];
	});
}

</script>
